// JavaScript Document
/**
 * 下拉页面加载文章
 */
$(document).ready(function(){
    $(window).scroll(function(){
        if($('.loading-article').length > 0){
            return;
        }
        var range = 30;
        var totalHeight = parseFloat($(window).height()) + parseFloat($(window).scrollTop());
        if(totalHeight >= $(document).height() - 30){
            $.ajax({
                url: '/api/article',
                method: 'get',
                contentType : 'application/json;charset=utf8',
                data:'',
                dataType: 'json',
                beforeSend: function(){
                    $('.recommend-panel').append('<div class="loading-article">' +
                                                 '<img src="/static/image/loading_article.gif"/><span>加载中...请稍等</span>' +
                                                 '</div>');
                },
                success: function(data){
                    var articles = data.articles;
                    setTimeout(function(){
                        $('.loading-article').remove();
                        $.each(articles, function(i, article){
                            $('.recommend-panel').append('<div class="article-window">' +
                                '<h3 class="article-title"><a href="/' + article.art_username + '/article/details/' + article.art_id + '">' + article.art_title + '</a></h3>' +
                                '<div class="article-summary">' + excludeSpecial(article.art_content) + '</div>' +
                                '<dl class="article-info">' +
                                '<dt><a href="#"><img src="' + article.art_headurl + '"/></a></dt>' +
                                '<dd class="article-nickname"><a href="#">' + article.art_nickname + '</a></dd>' +
                                '<dd class="article-time">' + article.create_time + '</dd>' +
                                '<div class="floatR">' +
                                '<dd class="article-readnum"><a href="#"><span class="text">阅读数</span><span class="read-num">999</span></a></dd>' +
                                '<dd class="article-evalnum"><a href="#"><span class="text">评价</span><span class="eval-num">' + article.art_comnum + '</span></a></dd>' +
                                '</div></dl></div>');
                        });
                    }, 2000);
                },
                error: function(){
                }
            });
        }
    });
});

/**
 * 过滤html标签
 * @param c
 * @returns {*}
 */
var excludeSpecial = function(c){
    // c = c.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
    // c = c.replace(/[\@\#\$\%\^\&\*\(\)\{\}\:\"\L\<\>\?\[\]]/g, '');
    // c = c.replace(/[\&lt;\&gt;\&nbsp;\&amp;\&quot;\&acute;]/g, '');
    c = c.replace(/&lt;\/?.+?\/?&gt;/g, '');
    c = c.replace(/<\/?.+?\/?>/g, '');
    return c;
}
/**
 * 初始化过滤文章本文展示
 */
$(function(){
    var summary = document.getElementsByClassName("article-summary");
    for(i=0;i<summary.length;i++){
        var html = summary[i].innerHTML;
        summary[i].innerHTML = (excludeSpecial(html));
    }
});